package cn.dnui_cr503.dnuifood_16110100503.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chengrui on 2018/11/14.
 */

public abstract class RecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH>{
    protected int layoutResource;
    protected Context context;
    protected List data=new ArrayList();
    protected LayoutInflater layoutInflater;

    public RecyclerViewAdapter(Context context,List data,int layoutResource){
        this.layoutResource=layoutResource;
        this.context=context;
        this.data.addAll(data);
        layoutInflater=LayoutInflater.from(context);
    }

    @Override
    public VH onCreateViewHolder(ViewGroup viewGroup, int i) {
        if(getClass().getGenericSuperclass()instanceof ParameterizedType){
            ParameterizedType parameterizedType=(ParameterizedType)getClass().getGenericSuperclass();
            Class cls=(Class)parameterizedType.getActualTypeArguments()[0];
            View view=layoutInflater.inflate(layoutResource,viewGroup,false);
            try {
                Constructor<VH> constructor=cls.getConstructor(View.class);
                return constructor.newInstance(view);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {

    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    public void setData(List data){
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }
}
